<?php
/***************************************************************************
*                            Dolphin Smart Community Builder
*                              -------------------
*     begin                : Mon Mar 23 2006
*     copyright            : (C) 2007 BoonEx Group
*     website              : http://www.boonex.com
* This file is part of Dolphin - Smart Community Builder
*
* Dolphin is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the
* License, or  any later version.
*
* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Dolphin,
* see license.txt file; if not, write to marketing@boonex.com
***************************************************************************/

bx_import('BxDolTwigPageView');

class BxLocationServicePageView extends BxDolTwigPageView {	

    function BxLocationServicePageView(&$oServicesMain, &$aService) {
        parent::BxDolTwigPageView('modzzz_location_services_view', $oServicesMain, $aService);
	
        $this->_oDb->_sTableMediaPrefix = $this->_oDb->_sTableServiceMediaPrefix; 
	
		switch($_GET['ajax']) {  
			case 'delete_review':
				$iLocationId = $_GET['id'];
				$iReviewId = $_GET['review'];
				$this->_oDb->DeleteReview($iLocationId, $iReviewId, 'service'); 
				echo '';
				exit;
			break; 
		}		
	
	}
 
	function getBlockCode_VideoEmbed() { 
 
	    $sVideoEmbed = trim($this->aDataEntry['video_embed']);
		
		if(!$sVideoEmbed)
			  return;
		
		$aVars = array ( 
			'embed' => $sVideoEmbed 
	    );
	
	    return $this->_oTemplate->parseHtmlByName('embed_block', $aVars);  
	}

    /**
     * Profile block with location event map 
     * @return html with location event map
     */         
  	function getBlockCode_Map() {

        $iEntryId = (int)$this->aDataEntry['id'];   
		$iAuthorId = (int)$this->aDataEntry['author_id'];   
	 
        $r = $this->_oDb->getSubProfileById('service', $iEntryId);
        $sBoxContent = '';
        if ($r && $this->_oMain->isAllowedViewSubLocation ('service', $iEntryId, $r)) {
 
			$sBaseUri = BX_DOL_URL_ROOT . $this->_oMain->_oConfig->getBaseUri();

			$this->_oTemplate->addInjection('injection_body', 'text',  'onload="load(\'service\', \''.$sBaseUri.'\', '.$this->aDataEntry['id'].', '.$r['lat'].', '.$r['lng'].')"'); 
			$this->_oTemplate->addJs ('http://maps.google.com/maps/api/js?sensor=false');
			$this->_oTemplate->addJs ('sub_gmap_single.js');
 
            $aVars = array (
                'text' => $r['address'] ? $r['address'] : _t('_modzzz_auto_the_same_address'), 
            );
            $sBoxContent = $this->_oTemplate->parseHtmlByName('block_map_single', $aVars);
		}
  
		if ($sBoxContent || $sBoxFooter)
			return array($sBoxContent, array(), $sBoxFooter);
		return '';

	}
 
	function getBlockCode_Info() {
        return $this->_oTemplate->blockSubProfileInfo ('service', $this->aDataEntry);
    }

	function getBlockCode_Desc() {
        return $this->_oTemplate->blockDesc ($this->aDataEntry);
    }

    function getBlockCode_Photos() {

		$this->_oDb->_sTableMediaPrefix = $this->_oDb->_sTableServiceMediaPrefix;

        return $this->_blockPhoto ($this->_oDb->getMediaIds($this->aDataEntry['id'], 'images'), $this->aDataEntry['author_id']);
    }    

    function getBlockCode_Videos() {

		$this->_oDb->_sTableMediaPrefix = $this->_oDb->_sTableServiceMediaPrefix;

        return $this->_blockVideo ($this->_oDb->getMediaIds($this->aDataEntry['id'], 'videos'), $this->aDataEntry['author_id']);
    }    

    function getBlockCode_Sounds() {

		$this->_oDb->_sTableMediaPrefix = $this->_oDb->_sTableServiceMediaPrefix;

        return $this->_blockSound ($this->_oDb->getMediaIds($this->aDataEntry['id'], 'sounds'), $this->aDataEntry['author_id']);
    }    

    function getBlockCode_Files() {

		$this->_oDb->_sTableMediaPrefix = $this->_oDb->_sTableServiceMediaPrefix;

        return $this->_blockFiles ($this->_oDb->getMediaIds($this->aDataEntry['id'], 'files'), $this->aDataEntry['author_id']);
    }    
    function getBlockCode_Logo() {
 
        return $this->_oTemplate->blockLogo ($this->aDataEntry['photo']);
	}
    function getBlockCode_Rate() {
        modzzz_location_import('ServiceVoting');
        $o = new BxLocationServiceVoting ('modzzz_location_service', (int)$this->aDataEntry['id']);
    	if (!$o->isEnabled()) return '';
        return $o->getBigVoting ($this->_oMain->isAllowedRate($this->aDataEntry));
    }        

    function getBlockCode_Comments() {    
        modzzz_location_import('ServiceCmts');
        $o = new BxLocationServiceCmts ('modzzz_location_service', (int)$this->aDataEntry['id']);
        if (!$o->isEnabled()) 
            return '';
        return $o->getCommentsFirst ();
    }            

    function getBlockCode_Actions() {
        global $oFunctions;

        if ($this->_oMain->_iProfileId || $this->_oMain->isAdmin()) {
  
			$aServiceEntry = $this->_oDb->getServiceEntryById($this->aDataEntry['id']);
			$iEntryId = $aServiceEntry['location_id'];
	 
		    $aDataEntry = $this->_oDb->getEntryById($iEntryId);
   
            $this->aInfo = array (
                'BaseUri' => $this->_oMain->_oConfig->getBaseUri(),
                'iViewer' => $this->_oMain->_iProfileId,
                'ownerID' => (int)$this->aDataEntry['author_id'],
                'ID' => (int)$this->aDataEntry['id'],
                'URI' => $this->aDataEntry['uri'],
                'TitleEdit' => $this->_oMain->isAllowedSubEdit($aDataEntry, $aServiceEntry) ? _t('_modzzz_location_action_title_edit') : '',
                'TitleDelete' => $this->_oMain->isAllowedSubDelete($aDataEntry, $aServiceEntry) ? _t('_modzzz_location_action_title_delete') : '',
				'TitleReview' => $this->_oMain->isAllowedReview($aDataEntry) ? _t('_modzzz_location_action_title_review') : '', 
  
            );

            if (!$this->aInfo['TitleEdit'] && !$this->aInfo['TitleDelete'])
                return '';

            return $oFunctions->genObjectsActions($this->aInfo, 'modzzz_location_service');
        } 

        return '';
    }    
 
 
	function getBlockCode_Review() { 
		$iEntryId = $this->aDataEntry['id'];
 
		$iLimit = $this->_oDb->getParam('modzzz_location_perpage_review');
		$sUrl = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'service/view/' . $this->aDataEntry['uri'];
  
		$iCount = $this->_oDb->getReviewCount($iEntryId, 'service');  

		if(!$iCount)
			return;

        $sPaginate = ''; 
        if ($iCount) {
            $iPages = ceil($iCount/ $iLimit);
            $iPage = ( isset($_GET['page']) ) ? (int) $_GET['page'] : 1;

            if ( $iPage < 1 ) {
                $iPage = 1;
            }
            if ( $iPage > $iPages ) {
                $iPage = $iPages;
            }    

            $sqlFrom = ($iPage - 1) * $iLimit;
            $sqlLimit = "LIMIT {$sqlFrom}, {$iLimit}";  
		}
		$aAllEntries = $this->_oDb->getReviews($iEntryId, $sqlLimit, 'service');
 
		$aVars = array (
			'bx_repeat:review' => array (),
		);
  
		foreach($aAllEntries as $aEntry)
		{ 
			$iReviewId = (int)$aEntry['id'];
			$iMemberId = $aEntry['member_id'];
			$sNickName = getNickName($iMemberId);
			$sNickLink = getProfileLink($iMemberId);
			$icoMember = get_member_thumbnail($iMemberId, 'left');
			$sMessage = $aEntry['desc']; 
			$dtSent = date("M jS, Y g:i A", $aEntry['created']);
		 
			$bAdmin = $this->_oMain->isEntryAdmin($this->aDataEntry) || ($iMemberId==$this->_oMain->_iProfileId);

			$aVars['bx_repeat:review'][] = array (
				'entry_id' => $iEntryId, 
				'review_id' => $iReviewId, 
				'ico_member' => $icoMember, 
				'nicklink' => $sNickLink,
				'nickname' => $sNickName,
				'date' => $dtSent,
				'message' => $sMessage,
				'bx_if:allowdelete' => array( 
					'condition' =>  $bAdmin,
					'content' => array(
 						'entry_id' => $iEntryId, 
						'review_id' => $iReviewId, 
						'url' => $sUrl,   
					), 
				),	 

			);  
		} 
  
		$sCode = $this->_oTemplate->parseHtmlByName('subprofile_reviews', $aVars);
	
		if ($iPages > 1) {
		   $oPaginate = new BxDolPaginate(array(
				'page_url' => $sUrl,
				'count' => $iCount,
				'per_page' => $iLimit,
				'page' => $iPage,
				'per_page_changer' => true,
				'page_reloader' => true,
				'on_change_page' => 'return !loadDynamicBlock({id}, \''.$sUrl.'?page={page}&per_page={per_page}\');',
				'on_change_per_page' => ''
			)); 
			
			$sAjaxPaginate = $oPaginate->getSimplePaginate('',-1,-1, false); 
		}   	
  
		return array($sCode, array(), $sAjaxPaginate);
	}

    function getCode() { 
        return parent::getCode();
    }    
}

?>